<?php
/**
 * User: Ason
 * 考试管理
 */

class Exam extends Admin_controller {

	public $json = [ 'status'=>'error', 'error'=>'未知错误' ];	//返回状态
	
	public function __construct(){
		parent::__construct();
		parent::logged_check();
		$this->load->model( 'exam_model', 'exam' );
	}

	/**
	 * 考场清单、添加考场
	 */
	public function index(){

		$this->data[ 'page_name' ] = '考试管理';
		//获取所有考试场次
		$this->data[ 'exam' ] = $this->exam->get_all_exam();
		//获取所有班级
		$this->data[ 'class' ] = $this->exam->get_all_class();
		
		$this->view( 'exam/index' );
	}
	
	/**
	 * 添加考试场次ajax-post
	 * */
	public function add_exam(){
		
		$post = $this->input->post( null, true );
		//数据检查
		if( empty( $post['class'] ) ){
			$this->json[ 'error' ] = '考试班级不能为空';
		}elseif( empty( $post[ 'time' ] ) ){
			$this->json[ 'error' ] = '没指定考试时间';	
		}elseif( strtotime( $post[ 'time' ] ) <= time() ){
			$this->json[ 'error' ] = '指定的考试时间已过，请重新指定！';
		}else{
			//写入数据
			if( $this->exam->add_exam( $post ) ){
				$this->json[ 'status' ] = 'success';
				$this->json[ 'error' ] = '成功添加考试场次！';
			}else{
				$this->json[ 'error' ] = '数据库错误，请稍后重试！';
			}
		}
		echo json_encode( $this->json ); return;
	}
	
	/**
	 * 修改考场信息
	 * @param $id int 考试场次id
	 */
	public function edit_exam( $id ){
		
		$exam = $this->exam->get_exam( 'id', (int)$id );
		$data[ 'exam' ] = $exam[ 0 ];
		$data[ 'class' ] = $this->exam->get_all_class();
		$this->load->view( 'exam/part/edit_exam', $data );
	}
	/**
	 * 执行修改考场信息ajax-post
	 * @param post数据应该包含id,class,time
	 */
	public function do_edit_exam(){
		
		$post = $this->input->post( null, true );
		//数据检查
		if( empty( $post[ 'id' ] ) ){
			$this->json[ 'error' ] = 'ID为空，请重新操作！';
		}elseif( empty( $post['class'] ) ){
			$this->json[ 'error' ] = '考试班级不能为空';
		}elseif( empty( $post[ 'time' ] ) ){
			$this->json[ 'error' ] = '没指定考试时间';	
		}elseif( strtotime( $post[ 'time' ] ) <= time() ){
			$this->json[ 'error' ] = '指定的考试时间已过，请重新指定！';
		}else{
			//写入数据
			if( $this->exam->edit_exam( $post ) ){
				$this->json[ 'status' ] = 'success';
				$this->json[ 'error' ] = '成功修改考试信息！';
			}else{
				$this->json[ 'error' ] = '数据库错误，请稍后重试！';
			}
		}
		echo json_encode( $this->json ); return;
	}
	
	/**
	 * 删除考试场次
	 * */
	public function del_exam( $id ){
		
		if( $this->exam->del_exam( (int)$id ) ){
			$this->json[ 'status' ] = 'success';
			$this->json[ 'error' ] = '成功删除考试场次！';
		}else{
			$this->json[ 'error' ] = '数据库错误，请重新操作！';
		}
		echo json_encode( $this->json ); return;
	}
	
	/**
	 * 考场监控
	 * @param $id int 考场id
	 * */
	public function watch( $id ){
		
		$this->data[ 'page_name' ] = '考场监控';
		//本场考试信息
		$exam = $this->exam->get_exam( 'id', (int)$id );
		$this->data[ 'exam' ] = $exam[ 0 ];
		
		//本场考试所有考卷信息
		$this->data[ 'tests' ] = $this->exam->get_examinees( $this->data[ 'exam' ][ 'cid' ] );
		//遍历考场学生，生成【考试状态】
		foreach( $this->data[ 'tests' ] as $k=>$v ){
			if( $v['handin_time'] ){
				$this->data[ 'tests' ][ $k ][ 'status' ] = "<span class='label label-success'>已交卷</span>";
			}elseif( $v[ 'select_time' ] ){
				$this->data[ 'tests' ][ $k ][ 'status' ] = "<span class='label label-default'>已抽题</span>";
			}else{
				$this->data[ 'tests' ][ $k ][ 'status' ] = '';
			}
		}
		$this->view( 'exam/watch' );
	}
	
	/**
	 * 让指定考生重考ajax-get
	 * @param sid 考生考号
	 * */
	public function reset_exam( $sid ){
		
		$sid = (int)$sid;
		//删除已上交的文件
		$a_dir = $this->data[ 'setting' ][ 'a_dir' ] . $sid;
		if( is_dir( $a_dir ) ){
			//遍历文件夹，删除所有文件
			$this->load->helper( 'WL_file' );
			if( del_dir( $a_dir ) ){
				//删除记录
				if( $this->exam->del_test_paper_by_sid( $sid ) ){
					$this->json[ 'status' ] = 'success';
					$this->json[ 'error' ] = "已删除{$sid}的考试信息，请考生重新登录抽屉考试！";
				}else{
					$this->json[ 'error' ] = '删除记录出错，请重试！';
				}
			}else{
				$this->json[ 'error' ] = '删除上传文件失败！请检查服务器【' .$a_dir . '】目录权限后重试';
			}
		}else{
			//删除记录
			if( $this->exam->del_test_paper_by_sid( $sid ) ){
				$this->json[ 'status' ] = 'success';
				$this->json[ 'error' ] = "已删除{$sid}的考试信息，请考生重新登录抽屉考试！";
			}else{
				$this->json[ 'error' ] = '删除记录出错，请重试！';
			}
		}
		echo json_encode( $this->json );return;
	}
	
	/**
	 * 让指定考生强制交卷 ajax-post
	 * @param $sid int 学号
	 * */
	public function handin( $sid ){
		
		$test_paper = $this->exam->get_test_paper_by_sid( $sid );
		if( empty( $test_paper ) ){
			$this->json[ 'error' ] = '该考生还没登录抽题，不能强制交卷！';
		}else{
			if( $this->exam->handin( $sid ) ){
				$this->json[ 'status' ] = 'seccess';
				$this->json[ 'error' ] = $sid .' 强制交卷成功！';
			}else{
				$this->json[ 'error' ] = $sid . ' 强制交卷失败！请重试！';
			}
		}
		echo json_encode( $this->json ); return ;
	}
	
	/**
	 * 考场记录
	 * @param $sid int 考生号
	 * */
	public function remark( $sid ){
		
		$sid = (int)$sid;
		$data[ 'error' ] = '';	//错误信息
		$data[ 'test_paper' ] = '';	//试卷信息
		$test_paper = $this->exam->get_test_paper_by_sid( $sid );
		if( empty( $test_paper ) ){
			$data[ 'error' ] = '该考生还没登录抽题，不能填写考场记录！';	
		}else{
			$data[ 'test_paper' ] = $test_paper[0];
		}
		$this->load->view( 'exam/part/remark', $data );
	}
	/**
	 * 考场记录ajax-post
	 * */
	public function do_remark(){
		
		$post = $this->input->post( null, TRUE );
		if( $post['sid'] ){
			$info = array( 'remark'=>$post['remark'] );
			if( $this->exam->edit_test_paper( $post['sid'], $info ) ){
				$this->json[ 'status' ] = 'success';
				$this->json[ 'error' ] = '考场记录成功！';
			}else{
				$this->json[ 'error' ] = '数据库操作失败！请重新尝试！';
			}
		}else{
			$this->json[ 'error' ] = '没有指定考号！';
		}
		echo json_encode( $this->json ); return ;
	}
	
}